<?php
// My birthday :D Not sure about the time though
header('Expires: Thu, 14 Oct 1993 05:00:00 GMT');
header('Cache-Control: no-cache');
header('Pragma: no-cache');

// Start a session
session_start();

// Get the directories
define('DOC_ROOT', realpath(dirname(__FILE__) . '/../') . '/');
define('ADM_ROOT', realpath(dirname(__FILE__) . '/../admin/') . '/');
define('INCL_DIR', DOC_ROOT . 'includes/');
define('ADM_TEM_DIR', ADM_ROOT . 'template/');

define ('nn', "\r\n");
define('br', "<br />\r\n");


include INCL_DIR . 'functions.main.php'; 	// Has some of the main functions
include INCL_DIR . 'include.config.php'; 	// Database connetion information
include INCL_DIR . 'class.database.php'; 	// Extraction layer

// First connect to the database
$db = new Database($database);
// Unset the database information A.S.A.P., just so that
// it's harder for hackers to get it
unset($database);

include INCL_DIR . 'functions.da.php';		// For the functions that are just copied and pasted from other scripts.
include INCL_DIR . 'class.session.php';		// Handles the sessions (Database)
include INCL_DIR . 'class.user.php';		// Handles the user information (Login, Logout, etc)
include INCL_DIR . 'Savant3.php';			// Template files
include INCL_DIR . 'recaptchalib.php';		// The reCapthca library
include INCL_DIR . 'class.settings.php';	// Get the site's settings from the config table.

/*
	Hash the user's IP address.
	I don't want to store their actual IP address in the
	database. So I'll hash it for security. Should slow down
	anyone that gets their hands on the database.
*/
$_SERVER['REMOTE_ADDR'] = md5($_SERVER['REMOTE_ADDR']);

$user_sid = (isset($_SESSION['sid'])) ? $_SESSION['sid'] : null;

if ($db->last_err != false)
{
	gerror_page(
					'Database Error', 
					'There has been an error trying to connect or to select a database.' . nn . 
					'<br /><br /><br />' . nn . 
					$db->last_err
				);
}

if(get_magic_quotes_gpc())
{
	$_POST    = fix_slashes($_POST);
	$_GET     = fix_slashes($_GET);
	$_REQUEST = fix_slashes($_REQUEST);
	$_COOKIE  = fix_slashes($_COOKIE);
}

// Get the site's configuration
set::init();

// User Authentication
$user = new user($user_sid, $_SERVER['REMOTE_ADDR']);

// Let's renew their session so that they don't
// get logged out in 20 minutes.
if ($user->is_logged_in == true)
{
	session::renew($user_sid, $_SERVER['REMOTE_ADDR']);
}

// Add the trailing slash please!
define('TEM_DIR', DOC_ROOT . 'template/' . $template_folder_name . '/');

if (!is_dir(TEM_DIR))
{
	gerror_page(
		'Template Error', 
		'The specfied template folder does not exist.' . nn . 
		'<br />'
	);
}

$tpl = new Savant3(array(
	'template_path' => TEM_DIR
));

$tpl->tem_path = 'template/' . $template_folder_name . '/';
$tpl->is_logged_in = $user->is_logged_in;
$tpl->user = $user->user_info;
